package com.nondo.rdp.finance.endpoint.api.app.user;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.finance.domain.UserAuth;
import com.nondo.rdp.finance.service.UserAuthService;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController("apiAppUserAuth")
@RequestMapping("/api/app/user/auth")
public class UserAuthEndpoint {
    @Autowired
    UserAuthService userAuthService;

    @RequestMapping("/list")
    public View list(HttpServletRequest request, HttpServletResponse response) {
        View view = new View(20000, "获取列表成功");

        User user = UserUtils.getUser(request, response);

        Map result = new HashMap();

        Optional<UserAuth> userAuthIsExist = this.userAuthService.findByUser(user);
        if (userAuthIsExist.isPresent()) {
            UserAuth userAuth = userAuthIsExist.get();
            result.put("idAuth", userAuth.getIdentityAuth());
            result.put("masterAuth", userAuth.getMasterAuth());
            result.put("adviserAuth", userAuth.getAdviserAuth());
        } else {
            result.put("idAuth", 0);
            result.put("masterAuth", 0);
            result.put("adviserAuth", 0);
        }

        view.setData(result);

        return view;
    }

    @RequestMapping("/id")
    public View idCardAuth(HttpServletRequest request, HttpServletResponse response, @Validated(UserAuth.Group1.class) @RequestBody UserAuth userAuth, BindingResult result) {
        View view = new View(20000, "提交成功，请耐心等待审核");

        if (result.hasErrors()) {
            return View.ofError(ValidateUtil.first(result.getAllErrors()));
        }

        User user = UserUtils.getUser(request, response);

        UserAuth newUserAuth = new UserAuth();

        //首先查找数据库是否已经存在记录了
        Optional<UserAuth> userAuthExist = this.userAuthService.findByUser(user);
        if (userAuthExist.isPresent()) {
            newUserAuth = userAuthExist.get();

            if (newUserAuth.getIdentityAuth() == 1) {
                return View.ofError("实名认证正在审核中，请耐心等待");
            }
            if (newUserAuth.getIdentityAuth() == 2) {
                return View.ofError("实名认证已审核通过，请勿重复提交");
            }

            //如果已经存在其他审核了，要校验审核的身份证号码是否一致
            if (!newUserAuth.getIdCardNo().isEmpty() && newUserAuth.getIdCardNo().equals(userAuth.getIdCardNo())) {
                return View.ofError("实名认证的身份证号必须和其他认证的身份证号码保持一致");
            }

        } else {
            newUserAuth.setUser(user);
        }
        newUserAuth.setIdCardFace(userAuth.getIdCardFace());
        newUserAuth.setIdCardBack(userAuth.getIdCardBack());
        newUserAuth.setIdCardNo(userAuth.getIdCardNo());
        newUserAuth.setIdentityAuth(UserAuth.PROGRESS_AUDITING);

        this.userAuthService.save(newUserAuth);

        return view;
    }

    @RequestMapping("/master")
    public View masterAuth(HttpServletRequest request, HttpServletResponse response, @Validated(UserAuth.Group2.class) @RequestBody UserAuth userAuth, BindingResult result) {
        View view = new View(20000, "提交成功，请耐心等待审核");

        if (result.hasErrors()) {
            return View.ofError(ValidateUtil.first(result.getAllErrors()));
        }

        User user = UserUtils.getUser(request, response);

        UserAuth newUserAuth = new UserAuth();

        //首先查找数据库是否已经存在记录了
        Optional<UserAuth> userAuthExist = this.userAuthService.findByUser(user);
        if (userAuthExist.isPresent()) {
            newUserAuth = userAuthExist.get();

            //防止多次提交
            if (newUserAuth.getMasterAuth() == 1) {
                return View.ofError("达人认证正在审核中，请耐心等待");
            }

            //达人认证和投顾认证只能存在一个
            if (newUserAuth.getAdviserAuth() == 1 || newUserAuth.getAdviserAuth() == 2) {
                return View.ofError("你已申请了投顾认证，无法申请达人认证");
            }

            //身份证号码必须和实名认证的身份证号码一直
            if (!newUserAuth.getIdCardNo().isEmpty() && !newUserAuth.getIdCardNo().equals(userAuth.getIdCardNo())) {
                return View.ofError("达人认证和实名认证的身份证必须是同一人");
            }
        } else {
            newUserAuth.setUser(user);
        }
        newUserAuth.setRealname(userAuth.getRealname());//真实姓名
        newUserAuth.setIdCardNo(userAuth.getIdCardNo());//身份证号
        newUserAuth.setPhone(userAuth.getPhone());//联系方式
        newUserAuth.setQq(userAuth.getQq());//qq

        newUserAuth.setProfession(userAuth.getProfession());//职业
        newUserAuth.setInvestmentTime(userAuth.getInvestmentTime());//投资年限
        newUserAuth.setEmail(userAuth.getEmail());//邮箱
        newUserAuth.setCompany(userAuth.getCompany());//公司
        newUserAuth.setPersonalAdvantage(userAuth.getPersonalAdvantage());//个人优势

        newUserAuth.setMasterAuth(UserAuth.PROGRESS_AUDITING);

        this.userAuthService.save(newUserAuth);

        return view;
    }

    @RequestMapping("/adviser")
    public View adviserAuth(HttpServletRequest request, HttpServletResponse response, @Validated(UserAuth.Group3.class) @RequestBody UserAuth userAuth, BindingResult result) {
        View view = new View(20000, "提交成功，请耐心等待审核");

        if (result.hasErrors()) {
            return View.ofError(ValidateUtil.first(result.getAllErrors()));
        }

        User user = UserUtils.getUser(request, response);

        UserAuth newUserAuth = new UserAuth();

        //首先查找数据库是否已经存在记录了
        Optional<UserAuth> userAuthExist = this.userAuthService.findByUser(user);
        if (userAuthExist.isPresent()) {
            newUserAuth = userAuthExist.get();

            //防止多次提交
            if (newUserAuth.getAdviserAuth() == 1) {
                return View.ofError("投顾认证正在审核中，请耐心等待");
            }

            //达人认证和投顾认证只能存在一个
            if (newUserAuth.getMasterAuth() == 1 || newUserAuth.getMasterAuth() == 2) {
                return View.ofError("你已申请了达人认证，无法申请投顾认证");
            }

            //身份证号码必须和实名认证的身份证号码一直
            if (!newUserAuth.getIdCardNo().isEmpty() && !newUserAuth.getIdCardNo().equals(userAuth.getIdCardNo())) {
                return View.ofError("投顾认证和实名认证的身份证必须是同一人");
            }
        } else {
            newUserAuth.setUser(user);
        }
        newUserAuth.setRealname(userAuth.getRealname());//真实姓名
        newUserAuth.setIdCardNo(userAuth.getIdCardNo());//身份证号
        newUserAuth.setPhone(userAuth.getPhone());//联系方式
        newUserAuth.setQq(userAuth.getQq());//qq

        newUserAuth.setCompany(userAuth.getCompany());//公司名字
        newUserAuth.setPosition(userAuth.getPosition());//岗位
        newUserAuth.setQualificationCertNo(userAuth.getQualificationCertNo());//资格证号
        newUserAuth.setInvestmentTime(userAuth.getInvestmentTime());//从业年限
        newUserAuth.setEmail(userAuth.getEmail());//邮箱

        newUserAuth.setPersonalAdvantage(userAuth.getPersonalAdvantage());

        newUserAuth.setAdviserAuth(UserAuth.PROGRESS_AUDITING);

        this.userAuthService.save(newUserAuth);

        return view;
    }
}